package com.cbwl.eoas.common.framework.utils;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.cbwl.eoas.common.web.Result;

public final class JSONUtil {
private static final Logger LOG = LoggerFactory.getLogger(JSONUtil.class);
    private static final SerializeConfig config = new SerializeConfig();
    private static final SerializerFeature[] features;

    public JSONUtil() {
    }

    public static synchronized <T> T getDTO(String jsonString, Class<T> clazz) {
        try {
            return JSON.parseObject(jsonString, clazz);
        } catch (Exception var3) {
            LOG.error(ExceptionUtils.getTrace(var3));
            return null;
        }
    }

    public static synchronized <T> T getDTO(String jsonString, TypeReference<T> type) {
        try {
            return JSON.parseObject(jsonString, type, new Feature[0]);
        } catch (Exception var3) {
            LOG.error(ExceptionUtils.getTrace(var3));
            return null;
        }
    }

    public static synchronized <T> Object[] getDTOArray(String jsonString, Class<T> clazz) {
        return JSON.parseArray(jsonString, clazz).toArray();
    }

    public static synchronized <T> List<T> getDTOList(String jsonString, Class<T> clazz) {
        return JSON.parseArray(jsonString, clazz);
    }

    public static synchronized Map<String, Object> getMapFromJson(String jsonString) {
        return JSON.parseObject(jsonString);
    }

    public static synchronized List<Map<String, Object>> getMapListForJson(String jsonString) {
        return (List)JSON.parseObject(jsonString, new TypeReference<List<Map<String, Object>>>() {
        }, new Feature[0]);
    }

    public static synchronized String getJSONString(Object object) {
        return JSON.toJSONString(object, config, features);
    }

    static {
        config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        config.put(java.sql.Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        features = new SerializerFeature[]{SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullStringAsEmpty};
    }
    
  
    
    
}